package com.kjlink.internal.workreport.web;

import com.kjlink.internal.workreport.system.service.SystemService;
import org.springframework.web.servlet.AsyncHandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 登录验证
 *
 * @author Vic.xu
 * @description:
 * @date: 2021/8/20 0020 17:09
 */
public class AuthenticationIntercepter implements AsyncHandlerInterceptor {

    private SystemService systemService;

    public AuthenticationIntercepter(SystemService systemService) {
        this.systemService = systemService;
    }

    public AuthenticationIntercepter() {
    }

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
            throws Exception {
        if (systemService.loginStatus()) {
            return true;
        }
        // 只有当session和cookie中均不存在用户的时候才拦截去登录页面
        String loginUrl = "/login";
        loginUrl = "/v3/index.html#/login";
        response.sendRedirect(request.getContextPath() + loginUrl);
        return false;
    }
}
